<template>
	<HyCard title="职业信息">
		<HyForm :hyFormDefault="hyFormDefault" :hyFormConfig="hyFormConfig" :hyFormFields="hyFormFields" ref="hyFormRef" />
	</HyCard>
</template>
<script setup lang="ts">
import HyForm from '/@/components/HyForm/index.vue';
import HyCard from '/@/components/HyCard/index.vue';
import { useDict } from '/@/hooks/dict';

const formType: string = inject('formType', 'submit');

// 定义字典
const { incumbent_status, job_title } = useDict('incumbent_status', 'job_title');
//配置基本信息表单
const hyFormFields = computed(() => {
	return {
		unifiedSocialCreditcode: {
			label: '工作单位统一社会代码',
			type: 'input',
			required: true,
		},
		workPlace: {
			label: '工作单位',
			required: true,
			type: 'input',
		},
		workStartTime: {
			label: '工作开始时间',
			type: 'date',
			showFormat: 'YYYY-MM-DD',
			valueFormat: 'YYYY-MM-DD',
			required: true,
			placeholder: '从事数字化相关工作的开始时间',
			change: (e: any) => workTimeChange(e),
		},
		yearsOfService: {
			label: '工作年限',
			type: 'input',
			required: true,
			disabled: true,
			placeholder: '自动生成(当前时间减去工作开始时间)',
		},
		incumbentStatus: {
			label: '在职状态',
			type: 'select',
			options: incumbent_status.value,
			required: true,
			change: (val: any) => incumbentStatusChange(val),
		},
		timeOfRetirement: {
			label: '退休时间',
			type: 'date',
			showFormat: 'YYYY-MM-DD',
			valueFormat: 'YYYY-MM-DD',
			required: true,
			remove: removeTimeOfRetirement.value,
		},
		jobTitle: {
			label: '职称',
			required: true,
			options: job_title.value,
			type: 'select',
		},
		jobLevel: {
			label: '职级',
			type: 'input',
			required: true,
			placeholder: '若无职级则填无',
		},
		office: {
			label: '职务',
			type: 'input',
			required: true,
			placeholder: '有职务必填，无职务填无',
		},
		workExperience: {
			label: '个人经历',
			type: 'textarea',
			required: true,
			maxlength: 200,
			placeholder:
				'2003.07毕业于XX大学XX专业；2007.07—20xx.05 重庆xxx研究院设计师;20xx.05—20xx.03 重庆xxx有限公司技术经理;20xx.03—至今 重庆xxx有限公司副总经理',
		},
		projectExperience: {
			label: '项目经历',
			type: 'textarea',
			maxlength: 200,
			required: true,
			placeholder: '负责/参与重庆市xxx单位xxxx项目、重庆市xxx单位xxxx项目重庆市xxx单位xxxx项目等多个重点项目咨询设计、建设与研发工作',
		},
	};
});
const hyFormRef = ref();
//动态表单配置
const hyFormConfig = computed(() => {
	return {
		labelWidth: '120px',
		type: formType,
	};
});
const props = defineProps({
	detailData: {
		type: Object,
		default: () => {
			return {};
		},
	},
});
const hyFormDefault = ref({});
watch(
	() => props.detailData,
	(nv1) => {
		hyFormDefault.value = nv1;
	},
	{ immediate: true }
);

const workTimeChange = (date: any) => {
	if (date) {
		const today = new Date();
		const birth = new Date(date);
		let yearsOfService = today.getFullYear() - birth.getFullYear();
		const monthDifference = today.getMonth() - birth.getMonth();
		// 如果当前月份小于生日月份，或者当前月份等于生日月份但日期小于生日日期，则年龄减一
		if (monthDifference < 0 || (monthDifference === 0 && today.getDate() < birth.getDate())) {
			if (yearsOfService > 0) {
				yearsOfService--;
			} else {
				yearsOfService = 0;
			}
		}
		hyFormRef.value.hyForm.yearsOfService = yearsOfService;
	}
};
const removeTimeOfRetirement = ref(true);
const incumbentStatusChange = (val: any) => {
	if (val === '2') {
		removeTimeOfRetirement.value = false;
	} else {
		removeTimeOfRetirement.value = true;
	}
};
const check = async () => {
	const result = await hyFormRef.value.check();
	if (result) return true;
	return false;
};
const getData = () => {
	return hyFormRef.value.hyForm;
};
defineExpose({ getData, check });
</script>
<style scoped lang="scss"></style>
